1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package javax.swing.colorchooser;
27
28 import javax.swing.*;
29 import java.awt.*;
30 import java.awt.event.*;
31 import javax.swing.event.*;
32 import javax.swing.text.*;
33 import java.io.Serializable;
34
35
36
37
38
39
40
41 class SmartGridLayout implements LayoutManager, Serializable {
42
43 int rows = 2;
44 int columns = 2;
45 int xGap = 2;
46 int yGap = 2;
47 int componentCount = 0;
48 Component[][] layoutGrid;
49
50
51 public SmartGridLayout(int numColumns, int numRows) {
52 rows = numRows;
53 columns = numColumns;
54 layoutGrid = new Component[numColumns][numRows];
55
56 }
57
58
59 public void layoutContainer(Container c) {
60
61 buildLayoutGrid(c);
62
63 int[] rowHeights = new int[rows];
64 int[] columnWidths = new int[columns];
65
66 for (int row = 0; row < rows; row++) {
67 rowHeights[row] = computeRowHeight(row);
68 }
69
70 for (int column = 0; column < columns; column++) {
71 columnWidths[column] = computeColumnWidth(column);
72 }
73
74
75 Insets insets = c.getInsets();
76
77 if (c.getComponentOrientation().isLeftToRight()) {
78 int horizLoc = insets.left;
79 for (int column = 0; column < columns; column++) {
80 int vertLoc = insets.top;
81
82 for (int row = 0; row < rows; row++) {
83 Component current = layoutGrid[column][row];
84
85 current.setBounds(horizLoc, vertLoc, columnWidths[column], rowHeights[row]);
86
87 vertLoc += (rowHeights[row] + yGap);
88 }
89 horizLoc += (columnWidths[column] + xGap );
90 }
91 } else {
92 int horizLoc = c.getWidth() - insets.right;
93 for (int column = 0; column < columns; column++) {
94 int vertLoc = insets.top;
95 horizLoc -= columnWidths[column];
96
97 for (int row = 0; row < rows; row++) {
98 Component current = layoutGrid[column][row];
99
100 current.setBounds(horizLoc, vertLoc, columnWidths[column], rowHeights[row]);
101
102 vertLoc += (rowHeights[row] + yGap);
103 }
104 horizLoc -= xGap;
105 }
106 }
107
108
109
110 }
111
112 public Dimension minimumLayoutSize(Container c) {
113
114 buildLayoutGrid(c);
115 Insets insets = c.getInsets();
116
117
118
119 int height = 0;
120 int width = 0;
121
122 for (int row = 0; row < rows; row++) {
123 height += computeRowHeight(row);
124 }
125
126 for (int column = 0; column < columns; column++) {
127 width += computeColumnWidth(column);
128 }
129
130 height += (yGap * (rows - 1)) + insets.top + insets.bottom;
131 width += (xGap * (columns - 1)) + insets.right + insets.left;
132
133 return new Dimension(width, height);
134
135
136 }
137
138 public Dimension preferredLayoutSize(Container c) {
139 return minimumLayoutSize(c);
140 }
141
142
143 public void addLayoutComponent(String s, Component c) {}
144
145 public void removeLayoutComponent(Component c) {}
146
147
148 private void buildLayoutGrid(Container c) {
149
150 Component[] children = c.getComponents();
151
152 for (int componentCount = 0; componentCount < children.length; componentCount++) {
153
154 int row = 0;
155 int column = 0;
156
157 if (componentCount != 0) {
158 column = componentCount % columns;
159 row = (componentCount - column) / columns;
160 }
161
162
163
164 layoutGrid[column][row] = children[componentCount];
165 }
166 }
167
168 private int computeColumnWidth(int columnNum) {
169 int maxWidth = 1;
170 for (int row = 0; row < rows; row++) {
171 int width = layoutGrid[columnNum][row].getPreferredSize().width;
172 if (width > maxWidth) {
173 maxWidth = width;
174 }
175 }
176 return maxWidth;
177 }
178
179 private int computeRowHeight(int rowNum) {
180 int maxHeight = 1;
181 for (int column = 0; column < columns; column++) {
182 int height = layoutGrid[column][rowNum].getPreferredSize().height;
183 if (height > maxHeight) {
184 maxHeight = height;
185 }
186 }
187 return maxHeight;
188 }
189
190 }